#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   zlib.sh
# Version:    1.0
# Date:       2013/12/12
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    03系统安全功能-08其他安全功能-04为Systemz优化和支持zlib压缩库-运行example代码，测试zlib库
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/12/12
#             - zlib
# ----------------------------------------------------------------------

##! @TODO: zlib-devel
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function DoSetup()
{
    trap DoCleanup EXIT INT 

    IsRoot
    EchoResult "root"

    CmdCheck gcc

    ZLIBVER=`rpm -qa | grep zlib-devel | awk -F '-' '{print $3}'`
    local Z=`echo ${ZLIBVER} | tr -d '.'`
    [ -n "${ZLIBVER}" ]
    EchoResult "zlib-devel installed"

    TMPDIR=/tmp/zlib$$
    mkdir ${TMPDIR}
    EchoResult "mkdir ${TMPDIR}"

echo "zlib version ${ZLIBVER} = 0x${Z}0, compile flags = 0xa9
uncompress(): hello, hello!
gzread(): hello, hello!
gzgets() after gzseek:  hello!
inflate(): hello, hello!
large_inflate(): OK
after inflateSync(): hello, hello!
inflate with dictionary: hello, hello!" > ${TMPDIR}/exp.out

}

##! @TODO: gcc
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure
function DoTest()
{

    DIR="/usr/share/doc/zlib-devel-${ZLIBVER}"
    [ -e ${DIR}/example.c ]
    EchoResult "${DIR}/example.c exists"

    cd ${DIR}
    gcc example.c -L /usr/lib64 -lz -o example
    EchoResult "gcc command"

    [ -x example ] && ./example > ${TMPDIR}/act.out
    EchoResult "./example"
    
    diff ${TMPDIR}/exp.out ${TMPDIR}/act.out
    EchoResult "example exec result"
    
}

##! @TODO: cleanup, delete user
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function DoCleanup()
{
    [ -e ${TMPDIR} ] && rm -rf ${TMPDIR}
    [ -e ${DIR}/example ] && rm -rf ${DIR}/example
    [ -e ${DIR}/foo.gz ] && rm -rf ${DIR}/foo.gz
}

# include lib files

if [ -z "$SFROOT" ]
then
    echo "SFROOT is null, pls check"
    exit 1
fi

. ${SFROOT}/lib/Echo.sh
. ${SFROOT}/lib/Check.sh
. ${SFROOT}/testcases/Security/lib/UserOps.sh

export LANG=C

DoSetup
DoTest
EchoResult "testcase: check zlib"
echo ""
